const app = getApp();
const util = require('../../../utils/util.js');
const lbbService = require('../../../pages/service/service.js');
const lbbTaskService = require('../service/task-service.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        activeKey:0,
        active:0,

        pageNo:1,
        pageSize:10,
        loading: true,
        loaded: false,
    },
    tabChange(e){
        let that = this
        that.data.pageNo =1
        this.setData({
            active:e.detail.index,
        })
        that.getDemandList()
    },
    loadmore() {
        let that = this
        if(that.data.total>that.data.demandList.length) {
            that.setData({
                pageNo: that.data.pageNo+1
            })
            that.getDemandList()
        }
        
    },
    // 需求列表
    getDemandList(){
        let that = this
        let parmas = {
            pageNo:that.data.pageNo,
            pageSize:that.data.pageSize,
            status:that.data.active,
        }
        wx.showLoading({
          title: '加载中',
        })
        lbbTaskService.getPendingDemandList(parmas,function(res){
            if(that.data.pageNo==1) {
                let lists = [ ...res.data.records]
                if(lists.length==0) {
                    that.setData({
                        loading: false,
                        loaded: true,
                        TotalPage:res.data.pages,
                        total:res.data.total,
                        demandList:lists
                    })
                    wx.hideLoading()
                } else {
                    that.setData({
                        loading: false,
                        loaded: true,
                        TotalPage:res.data.pages,
                        total:res.data.total,
                        demandList:lists
                    })
                    wx.hideLoading()
                }
            } else {
                let lists = [...that.data.demandList, ...res.data.records]
                console.log(lists)
                if(lists.length==0) {
                    that.setData({
                        loading: false,
                        loaded: true,
                        TotalPage:res.data.pages,
                        total:res.data.total,
                        demandList:lists
                    })
                    wx.hideLoading()
                } else {
                    that.setData({
                        loading: false,
                        loaded: true,
                        TotalPage:res.data.pages,
                        total:res.data.total,
                        demandList:lists
                    })
                    wx.hideLoading()
                }
            }
        })
    },
    showModal(e) {
        let that = this
        wx.showModal({
          title: '处理任务',
          content: '是否确认处理?',
          complete: (res) => {
            if (res.cancel) {
              
            }
        
            if (res.confirm) {
              lbbTaskService.markInProgress({id:e.currentTarget.dataset.id,demandType:e.currentTarget.dataset.type},function(res){
                    that.getDemandList()
              })
            }
          }
        })
    }, 
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getDemandList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})